import functools

from flask import current_app
from flask import g
from flask import session
from iHome.models import User



def login_required(view_func):
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):

        # 获取用户登录信息
        user_id = session.get("user_id")
        # 判断用户是否登录
        user = None
        if user_id:
            try:

                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user=user
        g.user_id = user_id
        return view_func(*args,**kwargs)
    return wrapper

from werkzeug.routing import BaseConverter

class ReagxConvertr(BaseConverter):
    """自定义转换器"""
    def __init__(self, url_map, *args):

        super(ReagxConvertr, self).__init__(url_map)
        self.regex = args[0]